import { Base } from '../../utils/base.js'

class Order extends Base {


	constructor() {
		super();
		this._storageKeyName = 'newOrder';
	}


	/*下订单*/
	doOrder(param, callback) {
		var that = this;
		var allParams = {
			url: 'post/subOrder',
			type: 'POST',
      data: param ,
			sCallback: function (data) {
				callback && callback(data);
			},
			eCallback: function () {
			}
		};
		this.request(allParams);
	}


    /*
    * 拉起微信支付
    * params:
    * norderNumber - {int} 订单id
    * return：
    * callback - {obj} 回调方法 ，返回参数 可能值 -1:商品缺货等原因导致订单不能支付;  0: 支付失败或者支付取消；1:支付成功；
    * */
	execPay(data, callback) {
		var allParams = {
      url: 'post/repay',
			type: 'POST',
			data,
			sCallback: function (res) {
        console.log(res)
        data = res.data.data
				var timeStamp = data.timeStamp;
				if (timeStamp) { //可以支付
					wx.requestPayment({
						'timeStamp': timeStamp.toString(),
						'nonceStr': data.nonceStr,
						'package': data.package,
						'signType': data.signType,
						'paySign': data.paySign,
						success: function () {
							callback && callback(1);
						},
						fail: function () {
							callback && callback(0);
						}
					});
				}
				else {
					callback && callback(-1);
				}
			}
		};
		this.request(allParams);
	}

	/*获得订单的具体内容*/
	getOrderInfoById(id, callback) {
		var that = this;
		var allParams = {
			url: 'order/' + id,
			sCallback: function (data) {
				callback && callback(data);
			},
			eCallback: function () {

			}
		};
		this.request(allParams);
	}

	/*获得所有订单,pageIndex 从1开始*/
	// getOrders(pageIndex, callback) {

	// 	var allParams = {
	// 		url: 'order/by_user',
	// 		data: { page: pageIndex },
	// 		type: 'get',
	// 		sCallback: function (data) {
	// 			console.log(data);
	// 			callback && callback(data);
	// 		}
	// 	};
	// 	this.request(allParams);

	// }
  getOrders(session3rd, callback) {

    var allParams = {
      url: `getOrder?session3rd=${session3rd}`,
      sCallback: function (res) {
        // console.log(res);
        callback && callback(res);
      }
    }

    this.request(allParams);

  }

	/*本地缓存 保存／更新*/
	execSetStorageSync(data) {
		wx.setStorageSync(this._storageKeyName, data);
	};

	/*是否有新的订单*/
	hasNewOrder() {
		var flag = wx.getStorageSync(this._storageKeyName);
		return flag == true;
	}

}

export { Order };